/*
 * addGuestRoom.java
 *
 * Created on __DATE__, __TIME__
 */

package hotel.view;

import java.awt.event.ActionEvent;
import java.sql.Connection;

import hotel.dao.KeFangDao;
import hotel.model.KeFang;
import hotel.util.DbUtil;

import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class addGuestRoom extends javax.swing.JInternalFrame {
	DbUtil dbUtil = new DbUtil();
	KeFangDao guestRoomDao = new KeFangDao();
	public addGuestRoom() {
		initComponents();
	}
private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jLabel1.setFont(new Font("宋体", Font.PLAIN, 22));
num = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();

jLabel2.setFont(new Font("宋体", Font.PLAIN, 22));
type = new javax.swing.JTextField();
save = new javax.swing.JButton();

save.setFont(new Font("宋体", Font.PLAIN, 22));
reset = new javax.swing.JButton();


setClosable(true);
setTitle("添加房间信息");

jLabel1.setText("房间号");

num.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
numActionPerformed(evt);
}
});

jLabel2.setText("类型");

save.setText("保存");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});

reset.setText("重置");
reset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetActionPerformed(evt);
}
});

GroupLayout layout = new GroupLayout(getContentPane());
layout.setHorizontalGroup(
	layout.createParallelGroup(Alignment.TRAILING)
		.addGroup(layout.createSequentialGroup()
			.addContainerGap(40, Short.MAX_VALUE)
			.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
				.addGroup(layout.createSequentialGroup()
					.addGap(68)
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
							.addComponent(save)
							.addGap(86)
							.addComponent(reset))
						.addGroup(layout.createSequentialGroup()
							.addComponent(jLabel2)
							.addGap(93)
							.addComponent(type, GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGap(79))
				.addGroup(layout.createSequentialGroup()
					.addGap(57)
					.addComponent(jLabel1)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(num, GroupLayout.PREFERRED_SIZE, 194, GroupLayout.PREFERRED_SIZE)
					.addGap(79))))
);
layout.setVerticalGroup(
	layout.createParallelGroup(Alignment.LEADING)
		.addGroup(layout.createSequentialGroup()
			.addGap(70)
			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(jLabel1)
				.addComponent(num, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
			.addGap(38)
			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(jLabel2)
				.addComponent(type, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
			.addGap(45)
			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(save)
				.addComponent(reset))
			.addContainerGap())
);
getContentPane().setLayout(layout);

pack();
}	
	private void resetActionPerformed(ActionEvent evt) {
		reset();
	}
	private void saveActionPerformed(java.awt.event.ActionEvent evt) {
		String num = this.num.getText();
		String type = this.type.getText();
		if(num == null || "".equals(num)){
			JOptionPane.showMessageDialog(null, "请输入房间号");
			return;
		}
		if(type == null && "".equals(type)){
			JOptionPane.showMessageDialog(null, "请输入类型");
			return;
		}
		Connection con = null;
		try {
			con = dbUtil.getCon();
			KeFang g = guestRoomDao.findByRoomNum(con, Integer.parseInt(num));
			//判断房间号是否存在
			if(g != null){
				JOptionPane.showMessageDialog(null, "该房间已存在");
				return;
			}
			
			//封装对象
			KeFang guestRoom = new KeFang();
			guestRoom.setNum(Integer.parseInt(num));
			guestRoom.setState(0);
			guestRoom.setType(type);
			
			//保存数据
			int result = guestRoomDao.addGusteRoom(con, guestRoom);
			if(result != 0){
				JOptionPane.showMessageDialog(null, "添加成功");
			}else{
				JOptionPane.showMessageDialog(null, "添加失败");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "添加失败");
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(con);//关闭数据库连接
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private void reset(){
		this.num.setText("");
		this.type.setText("");
	}

	void numActionPerformed(java.awt.event.ActionEvent evt) {
	}
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField num;
private javax.swing.JButton reset;
private javax.swing.JButton save;
private javax.swing.JTextField type;
}